package utils

import (
	"encoding/json"
	"os"

	"github.com/desyang-hub/zinx_demo/zface"
)

type ApplicationContext struct {
	Name      string        `json:"name"` // Server Name
	Ip        string        `json:"ip"`   // Server Listener Ip
	Port      int           `json:"port"` // Server Listener Port
	TcpServer zface.IServer // 服务实例

	Version          string `json:"version"` // Server Version
	MaxPackagesSize  uint32 `json:"maxPackagesSize"`
	WorkerPoolSize   uint32 `json:"workerPoolSize"`
	MaxTaskQueueSize uint32 `json:"maxTaskQueueSize"`
}

var AppContext *ApplicationContext // 定义一个可供访问的全局变量

func (ctx *ApplicationContext) Reload() {
	data, err := os.ReadFile("conf/application.json")
	if err != nil {
		panic(err)
	}

	err = json.Unmarshal(data, &AppContext)

	if err != nil {
		panic(err)
	}
}

// 提供一个方法初始化应用上下文
func init() {
	AppContext = &ApplicationContext{
		Name:             "Zinx Server App",
		Ip:               "localhost",
		Port:             8848,
		Version:          "0.4.0",
		MaxPackagesSize:  0,
		WorkerPoolSize:   4,
		MaxTaskQueueSize: 1024,
	}

	AppContext.Reload()
}
